package com.coder.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coder.entity.MajorLib;
import com.coder.entity.Options;
import com.coder.mapper.MajorLibMapper;
import com.coder.service.MajorLibService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 王令
 */
@Service
public class MajorLibServiceImpl extends ServiceImpl<MajorLibMapper, MajorLib> implements MajorLibService {


    @Override
    public List<Options> options() {
        List<Options> options = new ArrayList<>();
        list(new QueryWrapper<MajorLib>().select("distinct major_type")).forEach(v -> {
            options.add(new Options().setValue(v.getMajorType()).setLabel(v.getMajorType()));
        });

        options.forEach(v -> {
            List<Options> os = new ArrayList<>();
            list(new QueryWrapper<MajorLib>().select("distinct category").eq("major_type", v.getValue())).forEach(va -> {
                os.add(new Options().setLabel(va.getCategory()).setValue(va.getCategory()));
            });
            v.setChildren(os);
        });
        return options;
    }

    @Override
    public List<MajorLib> pages(MajorLib majorLib) {
        return list(new QueryWrapper<MajorLib>().select()
                .eq(StrUtil.isNotBlank(majorLib.getMajorType()), "major_type", majorLib.getMajorType())
                .eq(StrUtil.isNotBlank(majorLib.getCategory()), "category", majorLib.getCategory()));
    }
}
